import asyncio
import websockets as ws
from websockets import ConnectionClosed
import wal

count = 0


def do_with_resp(rsp):
    print(rsp)
    if "wal" == rsp:
        wal.wake_up("34-6F-24-A6-08-C3")
    else:
        print("else")


async def hello():
    uri = "ws://192.168.3.4:8085/ws"

    while True:
        try:
            async with ws.connect(uri) as websocket:
                await websocket.send('start')
                while True:
                    try:
                        await websocket.send('start')
                        rec = await websocket.recv()
                        do_with_resp(rec)
                        await asyncio.sleep(5)
                    except ConnectionClosed as e:
                        print(e.code)
                        if e.code == 1006:
                            print('restart')
                            await asyncio.sleep(2)
                            break
        except ConnectionRefusedError as e:
            print(e)
            global count
            if count == 10:
                return
            count += 1
            await asyncio.sleep(2)


asyncio.get_event_loop().run_until_complete(hello())
